<?php
class classAction extends apiAction
{
	public $_mod = '';
	public $cache = '';
	public $cache_name = '';
    public function _initialize() {
		parent::_initialize();
		
    }
    
	public function index() {
		$id = $this->_get('id','intval');
		if($id===''){
			$data['msg'] = '课堂id不能为空';
			$data['status'] = 0;
			$data['data'] = null;
			$this->APIreturn($data);
		}
		$this->good_class($id);
    }
	
	public function test() {
		$id = $this->_get('id','intval');
		$map['class_list_id'] = $id;
		$map['status'] = 1;
		$field = 'title,content';
		$order = 'ordid asc,time desc';
		$list = D('daka_class_test')->field($field)->where($map)->order($order)->select();
		$data['msg'] = count($list)?'获取成功':'没有数据';
		$data['status'] = count($list)?1:0;
		$data['data'] = $list;
		$this->APIreturn($data);
    }
	public function rookie_class() {
		$map['status'] = 1;
		$order = 'ordid asc';
		$field = 'id,pid,title,type';
		$result = D('daka_class_list')->field($field)->where($map)->order($order)->select();
		$tree = new Tree();
        $tree->init($result);
        $list = $tree->get_tree_arr(0);
		if($list){
			krsort($list);
			$url = '';
			foreach($list as $key => $item){
				unset($list[$key]['tree_nbsp']);
				if($item['type']==1){
					$url = U('home/rookie/index',array('id'=>$item['id']));
				}
				$list[$key]['url'] = $url;
			}
			ksort($list);
			$list = array_values($list);
		}
		$data['msg'] = count($list)?'获取成功':'没有数据';
		$data['status'] = count($list)?1:0;
		$data['data'] = $list;
		$this->APIreturn($data);
    }
	public function rookie_detail() {
		$id = $this->_get('id', 'intval');
		if($id === ''){
			$data['msg'] = '讲座id不能为空';
			$data['status'] = 0;
			$data['data'] = null;
			$this->APIreturn($data);
		}
		
		$detail = D('daka_class_list')->where(array('id'=>$id))->find();
		
		$map['p.pid'] = $detail['pid'];
		$map['u.status'] = 1;//查询条件 是开启
		$map['p.status'] = 1;//查询条件 是开启
		$map['p.type'] = 1;//查询条件 必须是讲座
		
		$field = '*,p.id as pp_id,p.content as pp_content,p.type as pp_type';
		
	
		$U = D('daka_user');
		$P = D('daka_class_list');
		$detail = $P->table($P->getTableName().' p')->field($field)->join($U->getTableName().' u on u.id=p.uid')->where($map)->select();
		$json = array();
		$tmp = array();
		if($detail){
			foreach($detail as $key => $item){
				$tmp['id'] = $item['pp_id'];
				$tmp['title'] = $item['title'];
				$tmp['content'] = $item['pp_content'];
				$tmp['type'] = $item['type'];
				$tmp['career'] = $item['content'];
				$tmp['uname'] = $item['uname'];
				$tmp['photo'] = $item['photo'];
				$tmp['test_url'] = U('home/examination/index',array('id'=>$tmp['id']));
				$tmp['user_url'] = U('home/teacher/detail',array('id'=>$item['uid']));
				$tmp['paper_url'] = U('home/paper/detail',array('id'=>$item['paper_id']));
				if($tmp['id'] == $id){
					$tmp['class_num'] = 'cur';
					$tmp['class_on'] = 'on';
				}else{
					$tmp['class_num'] = '';
					$tmp['class_on'] = '';
				}
				$json[] = $tmp;
			}
		}
		
		
		$data['msg'] = count($json)?'获取成功':'没有数据';
		$data['status'] = count($json)?1:0;
		$data['data'] = $json;
		$this->APIreturn($data);
    }
	public function add_readnum()
    {
        //AJAX修改数据
        $mod = D('daka_paper');
        $id = $this->_get('id', 'intval');
        $field = 'read_num';
        $mod->where(array('id'=>$id))->setInc($field);
		
		//判断用户是否存在
		if(D('student')->where(array('openid'=>$this->user_info['openid']))->find()){
			
		}else{
			D('student')->add($this->user_info);
		}
		//给这个用户增加阅读数
		D('student')->where(array('openid'=>$this->user_info['openid']))->setInc($field);
        $data['msg'] = '成功';
		$data['status'] = 1;
		$data['data'] = array();
		$this->APIreturn($data);
    }
	public function add_sharenum()
    {
        //AJAX修改数据
        $mod = D('daka_paper');
        $id = $this->_get('id', 'intval');
        $field = 'share_num';
        $mod->where(array('id'=>$id))->setInc($field);
		
		//判断用户是否存在
		if(D('student')->where(array('openid'=>$this->user_info['openid']))->find()){
			
		}else{
			D('student')->add($this->user_info);
		}
		//给这个用户增加分享数
		D('student')->where(array('openid'=>$this->user_info['openid']))->setInc($field);
        $data['msg'] = '成功';
		$data['status'] = 1;
		$data['data'] = array();
		$this->APIreturn($data);
    }
	public function good_class($id=0,$keyword='') {
		$page = $this->_get('page','intval',1);//默认第一页
        $pagenum = $this->_get('pagenum','intval',10);//默认10条数据
		$time = $this->_get('time','intval',strtotime(date('Y-m-d',time()-60*24*3600)));//默认前60天 当天整点时间戳 开始的数据
		$map['p.time'] = array('gt',$time);//查询条件 大于time
		$id && $map['p.class_id'] = $id;//查询条件 id
		$keyword !== '' && $map['p.title'] = array('like','%'.$keyword.'%');//模糊查询
		$map['u.status'] = 1;//查询条件 是开启
		$map['p.status'] = 1;//查询条件 是开启
		$page = ($page - 1) * $pagenum;//分页处理
		$order = 'p.read_num desc,p.time desc';//分段排序
		$field = 'p.id,p.title,p.img,p.url,p.share_num,p.read_num,u.uname,p.is_hot';
		$U = D('daka_user');
		$P = D('daka_paper');
		$list = $P->table($P->getTableName().' p')->field($field)->join($U->getTableName().' u on u.id=p.uid')->where($map)->order($order)->limit($page,$pagenum)->select();
		
		
		$keyword !== '' && $data['total']  = $P->table($P->getTableName().' p')->join($U->getTableName().' u on u.id=p.uid')->where($map)->count();
			
		
        $data['msg'] = count($list)?'获取成功':'没有数据';
		$data['status'] = count($list)?1:0;
		$data['data'] = $list;
		$this->APIreturn($data);
    }
	
	public function search()
    {
        $keyword = $this->_get('keyword','trim');
		if($keyword===''){
			$data['msg'] = '关键词不能为空';
			$data['status'] = 0;
			$data['data'] = null;
			$this->APIreturn($data);
		}
        $this->good_class('',$keyword);
    }
	public function lately()
    {
		$map['status'] =1;
		$order = 'ordid asc,time desc';
		$field = 'name';
		$num = 7;
        $list = D('search')->field($field)->where($map)->limit($num)->select();
		$data['msg'] = count($list)?'获取成功':'没有数据';
		$data['status'] = count($list)?1:0;
		$data['data'] = $list;
		$this->APIreturn($data);
    }
	
	
	
}